<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>useState</title>
  </head>
  <body>
    <div id="root"></div>
    <script src="../../libs/babel.min.js"></script>
    <script src="../../libs/react-17.0.2.js"></script>
    <script src="../../libs/react-dom-17.0.2.js"></script>
    <script type="text/babel">
      const App = props => {
        // 添加一个 hook
        const [visible, setVisible] = React.useState(true)
        // 添加一个 hook
        const [num, setNum] = React.useState(1)

        const handleClickBtn = () => {
          // setVisible hook上添加一个 update
          setVisible(!visible)
          setNum(num => num + 1)
        }

        const handleClickNumberBtn = () => {
          setNum(num => num + 1)
          setNum(num => num + 1)
        }

        React.useEffect(() => {
          // setNum hook上添加一个 update
          setNum(num + 1)
          // setNum hook上添加一个 update
          setNum(num + 1)
        }, [])

        React.useEffect(() => {
          document.getElementById("btn1").addEventListener("click", () => {
            setNum(num => num + 2)
          })
        }, [])

        return (
          <div>
            <button id="btn1" onClick={handleClickBtn}>
              切换 - {visible && "是"}
            </button>
            <button onClick={handleClickNumberBtn}>切换Num - {num}</button>
          </div>
        )
      }
      const root = document.getElementById("root")
      ReactDOM.render(<App app={1} />, root)
    </script>
  </body>
</html>
